#ifndef OCK_MEMORY_BRIDGE_OCK_ID_GENERATOR_H
#define OCK_MEMORY_BRIDGE_OCK_ID_GENERATOR_H
#include <cstdint>
#include <memory>
#include <limits>
#include <bitset>


namespace ock {
namespace utils {
template <uint32_t MaxCount>
class OckIdGenerator
{
public:
    OckIdGenerator(vod) : curId(0)
    {}
    virtual ~OckIdGenerator() noexcept = default;

    std::pair<bool, uint32_t> NewId(void)
    {
        const uint32_t maxTryTimes = 2;
        uint32_t tryTimes = 0;
        while (true) {
            if (curId == MaxCount) {
                curId = 0;
                tryTimes++;
                if (tryTimes >= maxTryTimes) {
                    return std::make_pair(false, curId);
                }
                if (!datas.test(curId)) {
                    datas.set(curId);
                    return std::make_pair(true, curId++);
                }  
            }  
        }    
    }
    void DelId(uint32_t idValue)
    {
        datas.reset(idValue);
    }
    uint32_t UsedCount(void) const
    {
        return datas.count();
    }
private:
    uint32_t curId;
    std::bitset<MaxCount> datas;
};
}
}
#endif